<!DOCTYPE html>
<html>

<head>
    <title>游记列表</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css">
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/travel.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css">
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="js/common.js"></script>
    <script>
        $(function () {

            var params = getParams();
            //decodeURIComponent(); 用字符串格式获取url上的关键字
            var keyword = decodeURIComponent(params.keyword);
            //回显搜索的关键字
            $(".search p").html("您搜索的内容是:" + keyword);
            //获取url上的type
            var types = params.type;

            //发送请求获取总数
            $.get("/search/" + keyword + "/count", function (data) {
                //判断在何处查询,用type判断
                if (!types) {
                    //type为空,则是在主页

                    //如果攻略总数不为0 则触发攻略的点击事件
                    if (data.strategyCount != 0) {
                        //trigger触发某类事件
                        $("#strategy").trigger("click");
                    }
                    $("#strategy span").html("攻略[" + data.strategyCount + "]");

                    //如果攻略总数为0,游记总数不为0,则触发游记的点击事件
                    if (data.strategyCount == 0 & data.travelCount != 0) {
                        $("#travel").trigger("click");
                    }
                    $("#travel span").html("游记[" + data.travelCount + "]");

                    //如果攻略和游记的总数都为0,则触发日报的点击事件
                    if (data.strategyCount == 0 & data.travelCount == 0) {
                        $("#newsPage").trigger("click");
                    }
                    $("#newsPage span").html("日报[" + data.newsPageCount + "]");
                } else {
                    //type不为空

                    //当type=1,则触发攻略的点击事件并显示
                    if (types == 1) {
                        $("#strategy span").html("攻略[" + data.strategyCount + "]");
                        $("#strategy").trigger("click");
                    }
                    //当type=2,则触发游记的点击事件并显示
                    if (types == 2) {
                        $("#travel").trigger("click");
                        $("#travel span").html("游记[" + data.travelCount + "]");
                    }
                    //当type=3,则触发日报的点击事件并显示
                    if (types == 3) {
                        $("#newsPage").trigger("click");
                        $("#newsPage span").html("日报[" + data.newsPageCount + "]");
                    }
                }
            });


            var type;
            var pages = 2;//总页数
            $(".clickTab").click(function () {
                //清空点击对象下的数据
                $(".fade").html("")
                //获取该点击对象上的type值
                type = $(this).data("type");
                //发送请求,带上type和keyword,currentPage查询相关数据
                $.get("/search", {keyword: keyword, currentPage: 1, type: type}, function (data) {
                    //攻略
                    if (type == 1) {
                        if (data.total != 0) {
                            //查询总数不为0,拼上数据
                            var temp = "";
                            //循环数据
                            $.each(data.list, function (index, ele) {
                                //拼接数据
                                temp += "<div>" +
                                    "<div class=\"container commend\">" +
                                    "<a href=\"strategyCatalogs.html?id=" + ele.id + "\">" +
                                    "<p>" + ele.title + "</p>" +
                                    "<img src=" + ele.coverUrl + ">" +
                                    "</a>" +
                                    "</div>" +
                                    "<hr>" +
                                    "</div>"
                            })
                            $("#pills-strategy").append(temp);
                        } else {
                            $("#pills-strategy").html("您搜索的攻略内容不存在");
                        }
                    }

                    //游记
                    if (type == 2) {
                        //查询总数不为0,拼上数据
                        if (data.total != 0) {
                            var temp = "";
                            //循环数据
                            $.each(data.list, function (index, ele) {
                                //拼接数据
                                temp += "<div>" +
                                    "<div class=\"container commend\">" +
                                    "<a href=\"travelContent.html?id=" + ele.id + "\">" +
                                    "<p>" + ele.title + "</p>" +
                                    "<img src=" + ele.coverUrl + ">" +
                                    "</a>" +
                                    "</div>" +
                                    "<hr>" +
                                    "</div>"
                            })
                            $("#pills-travel").append(temp);
                        } else {
                            $("#pills-travel").html("您搜索的游记内容不存在");
                        }
                    }
                    //日报
                    if (type == 3) {
                        if (data.total != 0) {
                            var temp = "";
                            $.each(data.list, function (index, ele) {
                                temp += "<div>" +
                                    "<div class=\"container commend\">" +
                                    "<a href=\"newsContent.html?id=" + ele.id + "\">" +
                                    "<p>" + ele.title + "</p>" +
                                    "<img src=" + ele.coverUrl + ">" +
                                    "</a>" +
                                    "</div>" +
                                    "<hr>" +
                                    "</div>"
                            })
                            $("#pills-newsPage").append(temp);
                        } else {
                            $("#pills-newsPage").append("您搜索的内容不存在");
                        }
                    }
                    pages = data.pages;
                });
                currentPage = 2;//重置分页参数
            })

            var currentPage = 2;//当前页
            //搜索攻略
            function query() {
                $.get("/search", {keyword: keyword, currentPage: currentPage, type: type}, function (data) {
                    if (type == 1) {
                        var temp = "";
                        $.each(data.list, function (index, ele) {
                            temp += "<div>" +
                                "<div class=\"container commend\">" +
                                "<a href=\"strategyCatalogs.html?id=" + ele.id + "\">" +
                                "<p>" + ele.title + "</p>" +
                                "<img src=" + ele.coverUrl + ">" +
                                "</a>" +
                                "</div>" +
                                "<hr>" +
                                "</div>"
                        })
                        $("#pills-strategy").append(temp);
                        pages = data.pages;
                    }
                    if (type == 2) {
                        var temp = "";
                        $.each(data.list, function (index, ele) {
                            temp += "<div>" +
                                "<div class=\"container commend\">" +
                                "<a href=\"travelContent.html?id=" + ele.id + "\">" +
                                "<p>" + ele.title + "</p>" +
                                "<img src=" + ele.coverUrl + ">" +
                                "</a>" +
                                "</div>" +
                                "<hr>" +
                                "</div>"
                        })
                        $("#pills-travel").append(temp);
                        pages = data.pages;
                    }
                    if (type == 3) {
                        var temp = "";
                        $.each(data.list, function (index, ele) {
                            temp += "<div>" +
                                "<div class=\"container commend\">" +
                                "<a href=\"newsContent.html?id=" + ele.id + "\">" +
                                "<p>" + ele.title + "</p>" +
                                "<img src=" + ele.coverUrl + ">" +
                                "</a>" +
                                "</div>" +
                                "<hr>" +
                                "</div>"
                        })
                        $("#pills-newsPage").append(temp);
                        pages = data.pages;
                    }
                })
                currentPage++;
            }

            //监听滚动事件
            $(window).scroll(function () {
                if (($(document).scrollTop() + $(window).height()) >= $(document).height() - 1) {
                    if (currentPage <= pages) {
                        query();
                    } else {
                        $(document).dialog({
                            type: 'notice',
                            infoText: '已经到最底部了!',
                            autoClose: 1000,
                            position: 'bottom'  // center: 居中; bottom: 底部
                        });
                    }
                }
            });
        })
    </script>
</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:history.go(-1);">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-10">
            <div class="input-group-sm search">
                <p style="color: white; font-size: 20px"></p>
            </div>
        </div>
    </div>
</div>

<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab" role="tablist">
    <li class="nav-item" style="width: 33%">
        <a role="button" id="strategy" class="nav-link clickTab active show" data-type="1" data-toggle="pill"
           href="#pills-strategy">
            <span class="strategy"></span>
        </a>
    </li>
    <li class="nav-item" style="width: 33%">
        <a role="button" id="travel" class="nav-link clickTab" data-type="2" data-toggle="pill" href="#pills-travel">
            <span class="travel"></span>
        </a>
    </li>
    <li class="nav-item" style="width: 33%">
        <a role="button" id="newsPage" class="nav-link clickTab" data-type="3" data-toggle="pill"
           href="#pills-newsPage">
            <span class="newsPage"></span>
        </a>
    </li>

</ul>
<div class="tab-content" id="pills-tabContent">
    <div class="tab-pane fade active show" id="pills-strategy">
        <!--<div render-loop="list">
            <div class="container commend">
                <a data-href="strategyDetails.html?id=" render-fun="getHref" render-key="list.id">
                    <p render-html="list.title"></p>
                    <img render-src="list.coverUrl">
                </a>
            </div>
            <hr>
        </div>-->
    </div>
    <div class="tab-pane fade" id="pills-travel">
        <!--<div render-loop="list">
            <div class="container commend">
                <a data-href="travelContent.html?id=" render-fun="getHref" render-key="list.id">
                    <p render-html="list.title"></p>
                    <img render-src="list.coverUrl">
                </a>
            </div>
            <hr>
        </div>-->
    </div>
    <div class="tab-pane fade" id="pills-newsPage">
        <!--<div render-loop="list">
            <div class="container commend">
                <a data-href="newsContent.html?id=" render-fun="getHref" render-key="list.id">
                    <p render-html="list.title"></p>
                    <img render-src="list.coverUrl">
                </a>
            </div>
            <hr>
        </div>-->
    </div>
</div>
</body>

</html>